package com.cmw.controller.web;


import com.alibaba.fastjson.JSONObject;
import com.cmw.core.base.annotation.Description;
import com.cmw.core.base.exception.ServiceException;
import com.cmw.core.util.BeanUtil;
import com.cmw.core.util.PageHandler;
import com.cmw.core.util.StringHandler;
import com.cmw.entity.MemberEntity;
import com.cmw.service.inter.MemberService;
import com.netflix.appinfo.InstanceInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 会员基本信息  ACTION类
 * @author 程明卫
 * @date 2017-06-19T00:00:00
 */
@Description(remark="会员基本信息Controller",createDate="2017-06-19T00:00:00",author="程明卫")
@Api(value = "会员基本信息微服务", description = "MemberController 2017-06-19T00:00:00 程明卫")
@RestController
@RequestMapping({"/member"})
public class MemberController extends BaseController{
	@Resource(name="memberService")
	private MemberService memberService;

	@Autowired
	private DiscoveryClient discoveryClient;

	@GetMapping("/instance-info")
	public ServiceInstance showInfo(){
        ServiceInstance serviceInstance = this.discoveryClient.getLocalServiceInstance();
        return serviceInstance;
	}

	 /**
     * 跳转主页面
     * @param request
     * @param response
     * @return
     */
	 @ApiOperation("会员首页微服务")
    @GetMapping(value = "/home")
    public JSONObject home(HttpServletRequest request, HttpServletResponse response, Map<String,Object> map) throws Exception{
       List<MemberEntity> list = memberService.getListAll();
       return PageHandler.getJson(list);
    }

    /**
     * 跳转列表页面
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("会员信息列表微服务")
    @GetMapping(value = "/list")
    public JSONObject list(HttpServletRequest request, HttpServletResponse response){
        List<MemberEntity> list = memberService.getListAll();
        return PageHandler.getJson(list);
    }

    /**
     * 跳转详细页面
     * @return
     */
    @ApiOperation("根据会员ID获取会员信息")
    @GetMapping(value = "/info")
    public JSONObject info(@ApiParam("会员ID") @RequestParam Long id){
        if(!StringHandler.isValidObj(id)) throw new ServiceException(ServiceException.ID_IS_NULL);
        MemberEntity obj = memberService.get(id);
        return PageHandler.getJson(obj);
    }

    /**
     * 保存数据
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("保存会员")
    @PostMapping(value = "/save")
    public JSONObject save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        MemberEntity entity = BeanUtil.copyValue(MemberEntity.class, request);
        memberService.insert(entity);
        return PageHandler.getSuccessJson();
    }

    /**
     * 移除数据
     * @return
     */
    @ApiOperation("根据会员ID删除会员")
    @DeleteMapping(value = "/remove")
    public JSONObject remove(@ApiParam("会员ID") @RequestParam Long id) throws Exception{
        memberService.delete(id);
        return PageHandler.getSuccessJson();
    }
	
}
